<template>
  <div>
    <div>
        <el-text class="mx-1" type="success">当前线路：</el-text>
        <el-tag type="primary">{{curLink.name}}</el-tag>
        <el-button style="position: absolute;right: 30px;"
          size="small"
          type="primary"
          @click="showLink"
        >线路预览</el-button>
    </div>
    
    <el-divider />
    
    <el-row justify-content v-show="isVisible">
        <el-col :span="10"><el-text>名称</el-text></el-col>
        <el-col :span="10"><el-text>坐标</el-text></el-col>
        <el-col :span="4"><el-text>操作</el-text></el-col>
        <el-col :span="10"><el-tag>{{ newNode.name }}</el-tag></el-col>
        <el-col :span="10"><el-tag>{{ newNode.location }}</el-tag></el-col>
        <el-col :span="4"><el-button type="primary" size="small" @click="handleAdd">添加</el-button></el-col>
    </el-row>

    <el-table
    style="margin-top: 20px;"
      v-loading="loading"
      :data="nodesList"
      :default-sort="{ prop: 'serial', order: 'descending' }"
    >
      <el-table-column type="expand" width="30">
        <template #default="props">
            <el-descriptions border :column="3" size="small">
              <el-descriptions-item label="线路ID: ">{{ props.row.linkId }}</el-descriptions-item>
              <el-descriptions-item label="线路名称: ">{{ props.row.linkName }}</el-descriptions-item>
              <el-descriptions-item label="坐标:">{{ props.row.position }}</el-descriptions-item>
              <el-descriptions-item label="图片" align="center" >
                <image-preview  :src="props.row.thumbnail"/>
              </el-descriptions-item>
              <el-descriptions-item label="类型:"><dict-tag :options="map_node_type" :value="props.row.type"/></el-descriptions-item>
              <el-descriptions-item label="是否路线:"> <dict-tag :options="sys_yes_no" :value="props.row.isPath"/></el-descriptions-item>
              <el-descriptions-item label="备注:">{{ props.row.remark }}</el-descriptions-item>
              <el-descriptions-item label="笔记:">{{ props.row.note }}</el-descriptions-item>
              <el-descriptions-item label="其他:">{{ props.row.other }}</el-descriptions-item>
            </el-descriptions>
        </template>
      </el-table-column>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <!-- <el-table-column label="线路ID" align="center" prop="linkId" />
      <el-table-column label="线路名称" align="center" prop="linkName" /> -->
      <el-table-column label="序号" align="center" prop="serial" width="50"/>
      <el-table-column label="节点名称" align="center" prop="nodeName" />
      <!-- <el-table-column label="坐标" align="center" prop="position" />
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="map_node_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="是否线路" align="center" prop="isPath">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.isPath"/>
        </template>
      </el-table-column> -->
      
      <!-- <el-table-column label="笔记" align="center" prop="note" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="其他" align="center" prop="other" /> -->
      <!-- <el-table-column label="缩略图" align="center" prop="thumbnail" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.thumbnail" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['work:nodes:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['work:nodes:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改节点对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="nodesRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="线路ID" prop="linkId">
          <el-input v-model="form.linkId" disabled placeholder="请输入线路ID" />
        </el-form-item>
        <el-form-item label="线路名称" prop="linkName">
          <el-input v-model="form.linkName" disabled placeholder="请输入线路名称" />
        </el-form-item>
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="form.nodeName" disabled placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="是否线路" prop="isPath">
          <el-radio-group v-model="form.isPath">
            <el-radio
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="序号" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入序号" />
        </el-form-item>
        <el-form-item label="笔记" prop="note">
          <el-input v-model="form.note" placeholder="请输入笔记" />
        </el-form-item>
        <el-form-item label="坐标" prop="position">
          <el-input v-model="form.position" disabled placeholder="请输入坐标" />
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="dict in map_node_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="其他" prop="other">
          <el-input v-model="form.other" placeholder="请输入其他" />
        </el-form-item>
        <el-form-item label="缩略图" prop="thumbnail">
          <image-upload v-model="form.thumbnail"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>
<script setup>
import {ref} from "vue"
import { listNodes, getNodes, delNodes, addNodes, updateNodes } from "@/api/work/nodes"
const { proxy } = getCurrentInstance()
const { sys_yes_no, map_node_type } = proxy.useDict('sys_yes_no', 'map_node_type')
const props = defineProps(['curLink','newNode'])
const emit = defineEmits(['showLink'])

const nodesList = ref([])
const open = ref(false)
const title = ref("")
const curLink = ref({})
const newNode = ref({})
const isVisible= ref(false)
const loading = ref(true)
const total = ref(0)
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 100,
    linkId: null,
    linkName: null,
    nodeName: null,
    isPath: null,
    serial: null,
    note: null,
    position: null,
    type: null,
    other: null,
    thumbnail: null
  },
  rules: {
  }
})
const { queryParams, form, rules } = toRefs(data)

watchEffect(() => {
  // 在 3.5 之前只运行一次
  // 在 3.5+ 中在 "foo" prop 变化时重新执行
  curLink.value = props.curLink
  newNode.value = props.newNode
  if(props.newNode.name != null){
    isVisible.value = true
  }
  getList()
})

/** 预览数据 */
function showLink(){
    if(nodesList.value.length >= 2){
        emit('showLink', nodesList.value)
    }else{
        proxy.$modal.msgError("数据不存在")
    }
}

/** 查询节点列表 */
function getList() {
  if(curLink.value.id != null){
  loading.value = true
  queryParams.value.linkId=curLink.value.id
  
  listNodes(queryParams.value).then(response => {
    nodesList.value = response.rows
    total.value = response.total
    loading.value = false
  })}
  else{
    curLink.value.name = "无线路，请创建"
  }
}
/** 添加数据 */
function handleAdd(){
    if(curLink.value.id != null){
      if(newNode.value.name !=null){
        reset()
        form.value.nodeName=newNode.value.name
        form.value.serial=nodesList.value.length + 1
        form.value.position=newNode.value.location.toString()
        form.value.type = 0
        form.value.isPath = "Y"
        console.log(curLink.value,form.value)

        addNodes(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
        })
      }else{
        proxy.$modal.msgError("未选择节点")
      }
        
    }else{
        proxy.$modal.msgError("当前无线路，请创建")
    }
    
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getNodes(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改节点"
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["nodesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateNodes(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addNodes(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除节点编号为"' + _ids + '"的数据项？').then(function() {
    return delNodes(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    linkId: curLink.value.id,
    linkName: curLink.value.name,
    nodeName: null,
    isPath: null,
    serial: null,
    note: null,
    position: null,
    type: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    other: null,
    thumbnail: null
  }
  proxy.resetForm("nodesRef")
}

getList()

</script>
<style>
</style>